package android.slc.base.ui.widget.slider;

import javafx.beans.property.ReadOnlyIntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.event.EventHandler;
import javafx.scene.control.Slider;
import javafx.scene.input.MouseEvent;

public class SliderStepWrapper implements EventHandler<MouseEvent> {
    private final Slider slider;
    public final SimpleIntegerProperty valueProperty = new SimpleIntegerProperty();

    public SliderStepWrapper(Slider slider) {
        this.slider = slider;
    }

    public int getValue() {
        return valueProperty.get();
    }

    public ReadOnlyIntegerProperty getValueProperty() {
        return valueProperty;
    }

    public void applyStep() {
        this.slider.addEventHandler(MouseEvent.MOUSE_RELEASED, this);
    }

    public void abandonApplySearch() {
        this.slider.removeEventFilter(MouseEvent.MOUSE_RELEASED, this);
    }

    @Override
    public void handle(MouseEvent event) {
        if (MouseEvent.MOUSE_RELEASED == event.getEventType()) {
            int valueInt = (int) Math.round(slider.getValue());
            slider.setValue(valueInt);
            valueProperty.setValue(valueInt);
        }
    }
}
